<?php

require_once 'helper/DbTable.php';
class Group extends DbTable
{
    public static $_TABLE_NAME_ = 'yxb_group';
    public static $required = ['dept_id','gp_name'];
    public static $members_mapping = ['gp_id','dept_id','grouper_id','gp_name'];
    public static $relationship = ['yxb_groupship'];

    private $gp_id;
    private $dept_id;
    private $grouper_id;
    private $gp_name;

    public function copy()
    {
        // TODO: Implement copy() method.
        $temp = new self();
        foreach (self::$members_mapping as $item) {
            DbTable::callSetMethod($temp, $item, DbTable::callGetMethod($this, $item));
        }
        return $temp;
    }

    public function changeTo($obj)
    {
        // TODO: Implement changeTo() method.
        foreach (self::$members_mapping as $item) {
            DbTable::callSetMethod($this, $item, DbTable::callGetMethod($obj, $item));
        }
    }

    public static function getTableName()
    {
        // TODO: Implement getTableName() method.
        return self::$_TABLE_NAME_;
    }

    public static function getRequired()
    {
        // TODO: Implement getRequired() method.
        return self::$required;
    }

    public function loadData($data)
    {
        // TODO: Implement loadData() method.
        foreach (self::$members_mapping as $member) {
            debug($member);
            if (array_key_exists($member, $data)) {
                DbTable::callSetMethod($this, $member, $data[$member]);
            } else {
                return false;
            }
        }
        return true;
    }

    public function add()
    {
        // TODO: Implement add() method.
        $format = "insert into %s(%s) values(%s);";
        $attr = [];
        $values = [];
        foreach (self::$members_mapping as $item) {
            $value = DbTable::callGetMethod($this, $item);
            if ($item == "gp_id" || $value == null)
                continue;
            array_push($attr, $item);
            is_numeric($value) ? array_push($values, "$value") : array_push($values, "\"$value\"");
        }
        $sql = sprintf($format, self::$_TABLE_NAME_, join(",", $attr), join(",", $values));
        return $sql;
    }

    public function delete()
    {
        // TODO: Implement delete() method.
        $format = "delete from %s where gp_id=%s;";

        $sql = sprintf($format, self::$_TABLE_NAME_, $this->getGpId());
        return $sql;
    }

    public function edit($data)
    {
        // TODO: Implement edit() method.
        $format = "update %s set %s where gp_id=%s;";
        $changeSql = [];
        foreach (self::$members_mapping as $item) {
            if (isset($data[$item]) && $item != "gp_id") {
                DbTable::callsetMethod($this, $item, $data[$item]);
                is_numeric($data[$item]) ? array_push($changeSql, $item . " = " . $data[$item]) : array_push($changeSql, $item . "= \"" . $data[$item] . "\"");;
            }
        }
        $sql = null;
        if (count($changeSql) >= 1)
            $sql = sprintf($format, self::$_TABLE_NAME_, join(",", $changeSql), $this->getGpId());
        return $sql;
    }

    public static function query($condition)
    {
        debug($condition);
        // TODO: Implement query() method.
        if (isset($condition['name']) && isset($condition['value'])) {

            $format = "select * from %s where %s='%s';";
            $sql = sprintf($format, self::$_TABLE_NAME_, $condition['name'], $condition['value']);
            return $sql;
        }
        if (isset($condition['page']) && isset($condition['pageSize'])) {
            $page = (int)($condition['page']) - 1;
            $pageSize = (int)($condition['pageSize']);
            if ($page < 0)
                return null;

            $format = "select %s.*,yxb_department.dept_name from %s join yxb_department on yxb_department.dept_id=%s.dept_id ";
            if(isset($condition['condition']['dept_id']))
                $format.=" where yxb_group.dept_id= '".$condition['condition']['dept_id']."' ";
            $format.="limit %d,%d;";
            $sql = sprintf($format, self::$_TABLE_NAME_,self::$_TABLE_NAME_,self::$_TABLE_NAME_, $page * $pageSize, $pageSize);
            return $sql;
        }
        return null;
    }
    public function checkRelationship()
    {
        $sql = [];
        foreach (Group::$relationship as $item) {
            $format = "select * from %s where gp_id=%s;";
            array_push($sql, sprintf($format, $item, $this->getGpId()));
        }
        return $sql;

    }
    function __toString()
    {
        $data = array();
        foreach (self::$members_mapping as $item) {
            $key = DbTable::upperFirstName($item,true);
            $value = DbTable::callGetMethod($this, $item);
            $data[$key] = $value;
        }
        return json_encode($data);
    }

    /**
     * @return mixed
     */
    public function getGpId()
    {
        return $this->gp_id;
    }

    /**
     * @param mixed $gp_id
     */
    public function setGpId($gp_id)
    {
        $this->gp_id = $gp_id;
    }

    /**
     * @return mixed
     */
    public function getDeptId()
    {
        return $this->dept_id;
    }

    /**
     * @param mixed $dept_id
     */
    public function setDeptId($dept_id)
    {
        $this->dept_id = $dept_id;
    }

    /**
     * @return mixed
     */
    public function getGrouperId()
    {
        return $this->grouper_id;
    }

    /**
     * @param mixed $grouper_id
     */
    public function setGrouperId($grouper_id)
    {
        $this->grouper_id = $grouper_id;
    }

    /**
     * @return mixed
     */
    public function getGpName()
    {
        return $this->gp_name;
    }

    /**
     * @param mixed $gp_name
     */
    public function setGpName($gp_name)
    {
        $this->gp_name = $gp_name;
    }

}